package com.lh.nk;

/**
 * @author: linghao
 * @since: 2023/1/31
 *
 * 描述
 * 输入两个递增的链表，单个链表的长度为n，合并这两个链表并使新链表中的节点仍然是递增排序的
 */
public class 合并两个排序的链表NC33 {


    public ListNode Merge(ListNode list1,ListNode list2) {
        if(list1 == null)return list2;
        if(list2 == null)return list1;

        ListNode pHead = new ListNode(-1), cur = pHead;
        ListNode p1 = list1,p2 = list2;
        while(p1 != null && p2 != null){
            if(p1.val < p2.val){
                cur.next = p1;
                p1 = p1.next;
            }else{
                cur.next = p2;
                p2 = p2.next;
            }
            cur = cur.next;
        }
        //补足由于长度不一致，剩余没对比的节点
        if(p1 != null)cur.next = p1;
        if(p2 != null)cur.next = p2;
        return pHead.next;
    }
}

class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
